package com.googlecode.jstructs.runtime;

public class ValueTypeStack {
	private final long[] primitiveVariables = new long[1 << 16];
	private final Object[] objectVariables = new Object[1 << 16];
	private int index;

	private static final ThreadLocal<ValueTypeStack> current = new ThreadLocal<ValueTypeStack>() {
		protected ValueTypeStack initialValue() {
			return new ValueTypeStack();
		};
	};

	public static ValueTypeStack current() {
		return current.get();
	}

	public void pushInt(int v) {
		primitiveVariables[index++] = v;
	}

	public void pushLong(long v) {
		primitiveVariables[index++] = v;
	}

	public void pushFloat(float v) {
		primitiveVariables[index++] = Float.floatToRawIntBits(v);
	}

	public void pushDouble(double v) {
		primitiveVariables[index++] = Double.doubleToRawLongBits(v);
	}

	public void pushObject(Object v) {
		objectVariables[index++] = v;
	}

	public int popInt() {
		return (int) primitiveVariables[--index];
	}

	public long popLong() {
		return primitiveVariables[--index];
	}

	public float popFloat() {
		return Float.intBitsToFloat((int) primitiveVariables[--index]);
	}

	public double popDouble() {
		return Double.longBitsToDouble(primitiveVariables[--index]);
	}

	public Object popObject() {
		Object o = objectVariables[--index];
		objectVariables[index] = null;
		return o;
	}

}
